package org.study.es.boot.controller;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.study.es.boot.entity.BookEntity;
import org.study.es.boot.service.BookService;

/**
 * 参考 https://cloud.tencent.com/developer/article/1436465
 *
 * @author Administrator
 * @date 2021-12-31
 */
@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @ResponseBody
    @RequestMapping("/{id}")
    public BookEntity getBookById(@PathVariable(name = "id", required = true) String id) {
        BookEntity bookEntity = bookService.findById(id);
        log.info("当前信息：{}", JSON.toJSONString(bookEntity));
        return bookEntity;
    }

    @ResponseBody
    @RequestMapping("/save/{id}")
    public BookEntity save(@PathVariable(name = "id", required = true) String id) {
        BookEntity bookEntity = new BookEntity(id, "ES入门教程", "程裕强", "2018-10-01");
        log.info("当前信息：{}", JSON.toJSONString(bookEntity));
        BookEntity result = bookService.save(bookEntity);
        return result;
    }
}
